<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0">
  <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
  <title>添加学生</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    main .myform {
      width: 30%;
      margin-left: 40px;
    }

    main p {
      display: block;
      width: 100%;
      /* position: absolute;
      bottom: 20px; */
      text-align: center;
      margin: 20px 0;
    }

    .footer {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 10px;
      color: #666;
      text-align: center;
    }
  </style>
  <!-- Bootstrap -->
  <link rel="stylesheet" href="static/bootstrap-3.4.1/css/bootstrap.css">
  <link rel="stylesheet" href="static/bootstrap-3.4.1/css/bootoast.css">
  <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
  <script src="static/bootstrap-3.4.1/js/jquery-3.6.0.min.js"></script>
  <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  <script src="static/bootstrap-3.4.1/js/bootstrap.js"></script>
  <script src="./static/bootstrap-3.4.1/js/bootoast.js"></script>
  <script src="./static/js/types.js"></script>
</head>

<body>
<div class="container-fluid my-container">
  <main>
    <!-- 路径导航 -->
    <ol class="breadcrumb" style="margin-top:5px;">
      <li><a href="#">主页</a></li>
      <li><a href="#">学生管理</a></li>
      <li class="active">添加学生</li>
    </ol>
    <!-- 表单 -->
    <div class="myform">
      <form id="myForm">
        <!--        <div class="form-group">-->
        <!--          <label for="username">学<span style="color:transparent;user-select: none;">隐藏</span>号：</label>-->
        <!--          <input type="text" class="form-control" name="username" id="username" placeholder="请输入学号">-->
        <!--        </div>-->
        <div class="form-group">
          <label for="cName">姓<span style="color:transparent;user-select: none;">隐藏</span>名：</label>
          <input type="text" class="form-control" name="cName" id="cName" placeholder="请输入姓名">
        </div>
        <div class="form-group">
          <label for="cNUm">身份证号：</label>
          <input type="text" class="form-control" name="cNUm" id="cNUm" placeholder="请输入姓名">
        </div>
        <div class="form-group">
          <label for="cPhone">电<span style="color:transparent;user-select: none;">隐藏</span>话：</label>
          <input type="text" class="form-control" name="cPhone" id="cPhone" placeholder="请输入电话">
        </div>
        <div class="form-group">
          <label for="type">房<span style="color:transparent;user-select: none;">隐藏</span>型：</label>
          <select name="type" id="type" onblur="loadRooms()">

          </select>
        </div>
        <div class="form-group">
          <label for="rNum">房<span style="color:transparent;user-select: none;">隐藏</span>间：</label>
          <select name="rNum" id="rNum">

          </select>
        </div>
        <button type="button" class="btn btn-success" onclick="save()" id="save-btn">保存</button>
      </form>

    </div>

  </main>
  <!-- 尾部 -->
  <div class="footer">技术支持:西安粤嵌信息科技有限公司</div>
</div>
<script>
  $(function (){
    loadTypes();
  })
  function loadRooms(){
    let id = $("#type").val();
    $.getJSON("/room/selectByType",{"id":id},function(rooms){
      $("#rNum").empty();
      console.log(rooms);
      let option = `<option value="0" readonly="readonly">请选择房间</option>`
      for (let room of rooms){
        option += `<option value="${room.id}">${room.rNum}</option>`
      }
      $("#rNum").append(option);
    })
  }
  function save(){
    let cName = $("#cName").val();
    let cNum = $("#cNUm").val();
    let cPhone = $("#cPhone").val();
    let type = $("#type").val();
    let rNum = $("#rNum").val();
    let param = {
      "cName":cName,
      "cNum":cNum,
      "cPhone":cPhone,
      "rNum":rNum,
    }
    if (cName == null || cName == "" || cNum == null || cNum == "" || cPhone == "" || type == 0){
      bootoast({
        message:"信息不能为空",
        position:"top",
        type:"danger",
        timeout:2
      })
    }
    else {
      $.post("/customer/insertOne",param,function (data){
        if (data){
          bootoast({
            message:"添加成功",
            position:"top",
            type:"success",
            timeout:2
          })
        }else {
          bootoast({
            message:"添加失败",
            position:"top",
            type:"danger",
            timeout:2
          })
        }
      })
    }
  }
</script>
</body>

</html>